﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsGame1.CoreComponents;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace WindowsGame1.SpriteClasses
{
    public enum AnimationKey { Up, Down, Left, Right };

    public class AnimatedSprite : Sprite
    {
        List<Animation> animations = new List<Animation>();
        AnimationKey currentAnimation;
        bool isAnimating;
        float speed = 30.0f;

        public AnimatedSprite(Game game, Texture2D texture, List<Animation>
        animations)
            : base(game, texture)
        {
            spriteBatch = Game1.TileSpriteBatch;
            this.animations = animations;
            currentAnimation = AnimationKey.Down;
            isAnimating = false;
            width = animations[(int)currentAnimation].FrameWidth;
            height = animations[(int)currentAnimation].FrameHeight;
            center = new Vector2(width / 2, height / 2);
        }

        public float Speed
        {
            get { return speed; }
            set
            {
                speed = MathHelper.Clamp(value, 0.1f, 10f);
            }
        }

        public bool IsAnimating
        {
            get { return isAnimating; }
            set { isAnimating = value; }
        }

        public AnimationKey CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }

        public Rectangle CurrentRectangle
        {
            get
            {
                return animations[(int)currentAnimation].CurrentFrameRect;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (isAnimating)
                animations[(int)currentAnimation].Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
            SpriteSortMode.Deferred,
            SaveStateMode.None,
            Game1.Camera.TransformMatrix);
            spriteBatch.Draw(
            texture,
            Position,
            animations[(int)currentAnimation].CurrentFrameRect,
            Color.White);
            spriteBatch.End();
        }

        public void LockToMap()
        {
            if (position.X < 0)
                position.X = 0;
            if (position.Y < 0)
                position.Y = 0;
            if (position.X + width > TileMapComponent.WidthInPixels)
                position.X = TileMapComponent.WidthInPixels - width;
            if (position.Y + height > TileMapComponent.HeightInPixels)
                position.Y = TileMapComponent.HeightInPixels - height;
        }

        public void LockToCombatScreen()
        {
            if (position.X < 0)
                position.X = 0;
            if (position.Y < 0)
                position.Y = 0;
            if (position.X + width > 1030)
                position.X = 1030-width;
            if (position.Y + height > 665)
                position.Y = 665-width;
        }
    }
}